Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide proguard/r8 rules #1929

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Provide proguard/r8 rules #1929

wants to merge 1 commit into from

Conversation

ganadist
Copy link
Contributor

@ganadist ganadist commented Aug 5, 2021

Currently, developer needs to add proguard rules manually.
But rules can be provided as resource and proguard/r8 will read and apply automatically.

Rules are copied from https://github.com/google/gson/blob/master/examples/android-proguard-example/proguard.cfg

gson/src/main/resources/META-INF/proguard/gson.pro: proguard rules for java applications
gson/src/main/resources/META-INF/com.android.tools/proguard/gson.pro : proguard rules for Android Gradle Plugin
gson/src/main/resources/META-INF/com.android.tools/r8/gson.pro : R8 rules for Android Gradle Plugin

gson/src/main/resources/META-INF/com.android.tools/r8-from-3.0.0/gson.pro : Additional R8 rules for 3.0 or higher

@google-cla google-cla bot added the cla: yes label Aug 5, 2021
@ganadist ganadist force-pushed the proguard branch 2 times, most recently from 9353015 to f9a5f55 Compare August 10, 2021 06:10
@eamonnmcmanus
Copy link
Member

If these resources are present, does that mean that android-proguard-example can be made to work correctly even if its own copy of the proguard rules is deleted?

How do we test that these rules are working correctly?

@ganadist
Copy link
Contributor Author

ganadist commented Aug 11, 2021

Note: This repo needs git-lfs
Note2: This branch contains prebuilt gson from ganadist@830c5f664 : ganadist/minimal-reproducible-example@99a6075f7

$ git clone https://github.com/ganadist/VersionCodeDemo -b gson_proguard_rule_demo 
$ cd VersionCodeDemo

### use AGP 4.2
$ git checkout HEAD~1
$ ./gradlew :app:pPRUA
...
> Task :app:packageProductionReleaseUniversalApk

$ cat app/build/outputs/mapping/productionRelease/configuration.txt

### check whether META-INF/com.android.tools/r8/gson.pro is included

### use AGP 7.0
$ git checkout origin/gson_proguard_rule_demo
$ ./gradlew :app:pPRUA
...
> Task :app:packageProductionReleaseUniversalApk

$ cat app/build/outputs/mapping/productionRelease/configuration.txt

### check whether META-INF/com.android.tools/r8/gson.pro and META-INF/com.android.tools/r8-from-3.0.0/gson.pro are included 

See also

@eamonnmcmanus
Copy link
Member

Sorry, I should have been a bit more explicit about my concerns. This PR is adding 4 different ProGuard configuration files. Is there a way to add an automated test that will check that each of those files is correct? I think it will be hard to maintain the files otherwise.

@basurahan
Copy link

Any update to this one? r8 rules should be included its 2022 now

@Marcono1234
Copy link
Collaborator

Default rules have been added by #2397 and will be available in the next version, so this pull request here might be obsolete.

@ganadist and everyone else who wanted this feature, could you please try building Gson locally and see if the snapshot version of Gson correctly applies the rules.

Any feedback regarding these rules, such as if something is missing or is too coarse, is highly appreciated!

Also keep in mind that these default library rules cannot cover everything; if you have specific classes or fields which should be preserved and you are not using @SerializedName you might have to add additional rules for your build.

@Marcono1234 Marcono1234 added the proguard-r8 Issues relating to the use of ProGuard and/or R8, such as problems due to obfuscation label Mar 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes proguard-r8 Issues relating to the use of ProGuard and/or R8, such as problems due to obfuscation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants